/**
Copyright [2008] [Julian Gamble] 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 
	
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
 **/
package aerith.components;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;

import org.jdesktop.animation.timing.Animator;
import org.jdesktop.animation.timing.interpolation.PropertySetter;
import org.jdesktop.swingx.JXImagePanel;


public class AerithSplashScreen extends JLayeredPane {

	private static String logo = "/aerith-logo.png";

	/**
	 * 
	 */
	private static final long serialVersionUID = 3105481303455846934L;

	private JXImagePanel splashScreenImagePanel;

	private JComponent regularContentPanel;

	private JXImagePanel fadeOutImagePanel;

	private AerithSplashScreen layeredMyGlassPane;

	//private JFrame parentFrame;
	private JFrame parentFrame;

	public AerithSplashScreen() {
		super();
		this.layeredMyGlassPane = this;

	}

	public void fadeIn() {
		Animator animator = PropertySetter.createAnimator(1500,
				splashScreenImagePanel, "alpha", 1.0f);
		animator.setAcceleration(0.2f);
		animator.setDeceleration(0.3f);
		animator.start();
	}

	public void hideSplash() {
		int numberOfMillisecondsInTheFuture = 3000; // 10 sec
		Date timeToRun = new Date(System.currentTimeMillis()
				+ numberOfMillisecondsInTheFuture);
		Timer timer = new Timer();

		timer.schedule(new TimerTask() {
			public void run() {
				splashScreenImagePanel.setVisible(false);
				regularContentPanel.setVisible(true);
			}
		}, timeToRun);

		Animator animator = PropertySetter.createAnimator(1000,
				fadeOutImagePanel, "alpha", 0.0f);
		animator.setAcceleration(0.2f);
		animator.setDeceleration(0.3f);
		animator.setStartDelay(3000);
		animator.start();

		int numberOfMillisecondsInTheFuture2 = 4000; // 10 sec
		Date timeToRun2 = new Date(System.currentTimeMillis()
				+ numberOfMillisecondsInTheFuture2);
		Timer timer2 = new Timer();

		timer2.schedule(new TimerTask() {
			public void run() {
				layeredMyGlassPane.setVisible(false);
			}
		}, timeToRun2);
	}

	public void initSplashScreenImage() {
		Toolkit tk = Toolkit.getDefaultToolkit();
		System.out.println("logo = " + logo);
		Image coverArt= null;
		URL url = getClass().getResource(logo);
		if (url!=null)
			coverArt = tk.getImage(getClass().getResource(logo));
		else
			coverArt = tk.getImage(logo);
		//Image coverArt = tk
		//		.getImage(getClass().getResource(logo));

		splashScreenImagePanel = new AerithContentJXImagePanel();

		splashScreenImagePanel.setLayout(new GridLayout());
		splashScreenImagePanel.setAlpha(.01f);

		splashScreenImagePanel.setImage(coverArt);

		layeredMyGlassPane.add(splashScreenImagePanel, new Integer(1));
	}

	public void paint(Graphics g) {
		super.paint(g);

		doOnce();

		//parentFrame = (JFrame) this.getParent().getParent();
		parentFrame = (JFrame) this.getRootPane().getParent();

		// parentFrame = (JFrame)
		// WindowManager.getDefault().getMainWindow();

		regularContentPanel = (JComponent) parentFrame.getContentPane();

		//int WIDTH = parentFrame.getPreferredSize().width;
		int WIDTH = parentFrame.getWidth();
		if (WIDTH == 0) {
			WIDTH = 600;
		}
		//int HEIGHT = parentFrame.getPreferredSize().height;
		int HEIGHT = parentFrame.getHeight();
		if (HEIGHT == 0) {
			HEIGHT = 600;
		}

		splashScreenImagePanel.setBounds(regularContentPanel.getBounds().x,
				regularContentPanel.getBounds().y, WIDTH, HEIGHT);

		fadeOutImagePanel.setBounds(regularContentPanel.getBounds().x,
				regularContentPanel.getBounds().y, WIDTH, HEIGHT);

		layeredMyGlassPane.setPreferredSize(new Dimension(WIDTH, HEIGHT));
		//}
		//}

	}

	private boolean notLatched = true;

	private void doOnce() {

		if (notLatched) {
			layeredMyGlassPane.initSplashScreenImage();

			layeredMyGlassPane.initFadeOutImage();

			layeredMyGlassPane.fadeIn();

			layeredMyGlassPane.hideSplash();

			layeredMyGlassPane.fadeOut();

			notLatched = false;
		}

	}

	public void initFadeOutImage() {
		Toolkit tk = Toolkit.getDefaultToolkit();

		Image fadeOutImage = tk.getImage(getClass()
				.getResource("/FadeOut3.jpg"));
		// 5. Load the fade out panel
		fadeOutImagePanel = new JXImagePanel();
		fadeOutImagePanel.setLayout(new GridLayout());
		fadeOutImagePanel.setAlpha(.01f);

		fadeOutImagePanel.setImage(fadeOutImage);
		fadeOutImagePanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));

		layeredMyGlassPane.add(fadeOutImagePanel, new Integer(2));
	}

	public void fadeOut() {
		Animator animator = PropertySetter.createAnimator(1500,
				fadeOutImagePanel, "alpha", 1.0f);
		animator.setAcceleration(0.2f);
		animator.setDeceleration(0.3f);
		animator.setStartDelay(1500);
		animator.start();
	}
	
	public void setParentFrame(JFrame parentFrame2) {
		this.parentFrame = parentFrame2;
	}

	public static String getLogo() {
		return logo;
	}

	public static void setLogo(String logo) {
		AerithSplashScreen.logo = logo;
	}
}
